package org.deng.learn.mybatis.xml;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @Author DengLibin
 * @Date 19-4-27
 * @Time 下午7:37
 * @Desc 使用druid数据源在mybatis.cfg.xml中配置
 */
public class DruidDataSourceFactory extends UnpooledDataSourceFactory {

    private DruidDataSource dataSource = new DruidDataSource();

    /**
     * 复写该方法设置连接属性
     * @param properties
     */
    @Override
    public void setProperties(Properties properties) {
       dataSource.setUrl(properties.getProperty("url"));
       dataSource.setUsername(properties.getProperty("username"));
       dataSource.setPassword(properties.getProperty("password"));
       dataSource.setDriverClassName(properties.getProperty("driver"));
    }

    /**
     *复写该方法返回数据源
     * @return
     */
    @Override
    public DataSource getDataSource() {
        return  dataSource;
    }
}
